<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
        <title>Document</title>
        <style>
            #div1{width: 400px; height: 100px; background-color: orange}
            #div2{width: 400px; height: 500px; background-color: peachpuff}
            #input1 {width: 380px; height: 30px; font-size: 18px}
            #div1 button{width: 100px; height: 30px; font-size: 18px; background-color: black; color: white; margin-left: 5px; margin-top: 5px}
            #div2 div{border-bottom: 1px dashed gray; padding: 2px; position: relative;}
            #div2 div button{position: absolute; right: 0px}
        </style>
        <script src = 'tool.js'></script>
        <script>
            window.onload = function(){
                var oDiv1 = document.getElementById("div1");
                var oDiv2 = document.getElementById("div2");
                var oInput = document.getElementById("input1");

                //获取div1下面所有的button
                var aBtns = oDiv1.getElementsByTagName("button");

                //分别给三个按按钮添加不同的点击函数
                aBtns[0].onclick = function(){
                    if(!oInput.value){
                        alert("输入内容不能为空");
                    }else{
                        //获取到输入框的内容，创建节点添加到页面上
                        var newDiv = document.createElement("div");
                        var oTxt = document.createTextNode(oInput.value);
                        newDiv.appendChild(oTxt);
                        newDiv.style.backgroundColor = randomColor();
                        var newBtn = document.createElement("button");
                        newBtn.innerHTML = 'X';
                        newDiv.appendChild(newBtn);
                        //给新添加的按钮添加点击事件
                        newBtn.onclick = function(){
                            oDiv2.removeChild(this.parentNode);
                        }




                        oDiv2.appendChild(newDiv);
                        oInput.value = '';
                    }
                }

                aBtns[1].onclick = function(){
                    //删除最后一个节点
                    oDiv2.removeChild(oDiv2.lastChild);
                }

                aBtns[2].onclick = function(){
                    //拷贝最后一个节点，再添加到页面上
                    var newNode = oDiv2.lastChild.cloneNode(true);
                    oDiv2.appendChild(newNode);
                }
            }
        </script>
    </head>
    <body>
        <div id = 'div1'>
            <input type="text" id = 'input1' placeholder="输入内容"/><br>
            <button>增加</button>
            <button>删除</button>
            <button>拷贝</button>
        </div>
        <div id = 'div2'>
            <!-- <div>xxx <button>x</button></div>
            <div>xxx <button>x</button></div>
            <div>xxx <button>x</button></div> -->
        </div>
    </body>
</html>